﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

using Lottery.App;
using System.IO;

namespace UI
{
    public partial class MainFrm : Form
    {
        delegate void LoadDataHandler();

        public MainFrm()
        {
            InitializeComponent();

            tsl_time.Text = DateTime.Now.GetDateTimeFormats('D')[3];

            #region 列头颜色

            c_split1.HeaderCell.Style.BackColor = c_split2.HeaderCell.Style.BackColor = c_split3.HeaderCell.Style.BackColor = c_split4.HeaderCell.Style.BackColor = c_split5.HeaderCell.Style.BackColor = Color.Black;

            c_num1.HeaderCell.Style.ForeColor = Color.White;
            c_num2.HeaderCell.Style.ForeColor = Color.White;
            c_num3.HeaderCell.Style.ForeColor = Color.White;
            c_num4.HeaderCell.Style.ForeColor = Color.White;
            c_num5.HeaderCell.Style.ForeColor = Color.White;
            c_num6.HeaderCell.Style.ForeColor = Color.White;

            c_num7.HeaderCell.Style.ForeColor = Color.White;
            c_num8.HeaderCell.Style.ForeColor = Color.White;
            c_num9.HeaderCell.Style.ForeColor = Color.White;
            c_num10.HeaderCell.Style.ForeColor = Color.White;
            c_num11.HeaderCell.Style.ForeColor = Color.White;
            c_num12.HeaderCell.Style.ForeColor = Color.White;

            c_num13.HeaderCell.Style.ForeColor = Color.White;
            c_num14.HeaderCell.Style.ForeColor = Color.White;
            c_num15.HeaderCell.Style.ForeColor = Color.White;
            c_num16.HeaderCell.Style.ForeColor = Color.White;
            c_num17.HeaderCell.Style.ForeColor = Color.White;
            c_num18.HeaderCell.Style.ForeColor = Color.White;

            c_num19.HeaderCell.Style.ForeColor = Color.White;
            c_num20.HeaderCell.Style.ForeColor = Color.White;


            c_num1.HeaderCell.Style.BackColor = Color.Red;
            c_num2.HeaderCell.Style.BackColor = Color.Red;
            c_num3.HeaderCell.Style.BackColor = Color.Red;
            c_num4.HeaderCell.Style.BackColor = Color.Red;
            c_num5.HeaderCell.Style.BackColor = Color.Red;
            c_num6.HeaderCell.Style.BackColor = Color.Red;

            c_num7.HeaderCell.Style.BackColor = Color.Blue;
            c_num8.HeaderCell.Style.BackColor = Color.Blue;
            c_num9.HeaderCell.Style.BackColor = Color.Blue;
            c_num10.HeaderCell.Style.BackColor = Color.Blue;
            c_num11.HeaderCell.Style.BackColor = Color.Blue;
            c_num12.HeaderCell.Style.BackColor = Color.Blue;

            c_num13.HeaderCell.Style.BackColor = Color.Green;
            c_num14.HeaderCell.Style.BackColor = Color.Green;
            c_num15.HeaderCell.Style.BackColor = Color.Green;
            c_num16.HeaderCell.Style.BackColor = Color.Green;
            c_num17.HeaderCell.Style.BackColor = Color.Green;
            c_num18.HeaderCell.Style.BackColor = Color.Green;

            c_num19.HeaderCell.Style.BackColor = Color.Red;
            c_num20.HeaderCell.Style.BackColor = Color.Red;
            #endregion
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            lb_time.Text = DateTime.Now.ToString("HH:mm:ss");
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            this.Hide();
            
            #region 验证是否授权

            if (!Checker.Valid())
            {
                MessageBox.Show("您的版本已超过授权的使用期限!您可以通过以下方式联系软件作者\n\nQQ:84825263\n\n长沙手机: 18274967383\n\n", "软件已超过使用期限");
                Application.Exit();
                return;
            }
            #endregion
            
            Cover c = new Cover();
            c.FormClosed += new FormClosedEventHandler(c_FormClosed);
            c.Show();
        }

        void c_FormClosed(object sender, FormClosedEventArgs e)
        {
            LoadingManager.Show();

            this.Show();

            Thread t = new Thread(ThreadLoadData);
            t.IsBackground = true;
            t.Start();
        }

        void ThreadLoadData()
        {
            if (!Directory.Exists("data"))
            {
                Directory.CreateDirectory("data");
            }

            foreach (string item in Directory.GetFiles("data"))
            {
                try
                {
                    File.Delete(item);
                }
                catch { }
            }

            int count = GlobalData.Records.Count;

            Invoke(new LoadDataHandler(Do_LoadData));
        }

        void Do_LoadData()
        {
            LoadGraph("N8", GlobalData.Records[GlobalData.Records.Count - 1].Num);

            LoadingManager.Hide();
        }

        private void MainFrm_Resize(object sender, EventArgs e)
        {
            dgv_List.Top = 55;
            dgv_List.Width = this.Width - 30;

            int height = this.Height - 178;
            dgv_List.Height = height + 40;

            if (height % 30 != 0)
            {
                dgv_List.Height -= height % 30;
            }
        }

        #region 各玩法统计窗口

        private void N1_Click(object sender, EventArgs e)
        {
            new TJ(GameMode.A1, "数投统计").Show();
        }

        private void P3_Click(object sender, EventArgs e)
        {
            new TJ(GameMode.P3, "前三组统计").Show();
        }

        private void B4P3_Click(object sender, EventArgs e)
        {
            new TJ(GameMode.A3, "前三直统计").Show();
        }

        private void N2_Click(object sender, EventArgs e)
        {
            new TJ(GameMode.N2, "任选二统计").Show();
        }

        private void D1T2_Click(object sender, EventArgs e)
        {
            //new TJ(GameMode.D1T2, "一胆二拖统计").Show();
            new TJDT(GameMode.D1T2, "任选二胆拖统计").Show();
        }

        private void N3_Click(object sender, EventArgs e)
        {
            new TJ(GameMode.N3, "任选三统计").Show();
        }

        private void B4Z3_Click(object sender, EventArgs e)
        {
            new TJ(GameMode.B4Z3, "买四中三统计").Show();
        }

        private void N4_Click(object sender, EventArgs e)
        {
            new TJ(GameMode.N4, "任选四统计").Show();
        }

        private void B5Z4_Click(object sender, EventArgs e)
        {
            new TJ(GameMode.B5Z4, "买五中四统计").Show();
        }

        private void N5_Click(object sender, EventArgs e)
        {
            new TJ(GameMode.N5, "任选五统计").Show();
        }

        private void B6Z5_Click(object sender, EventArgs e)
        {
            new TJ(GameMode.B6Z5, "买六中五统计").Show();
        }

        private void P2_Click(object sender, EventArgs e)
        {
            new TJ(GameMode.P2, "连二组统计").Show();
        }

        private void A2_Click(object sender, EventArgs e)
        {
            new TJ(GameMode.A2, "连二直统计").Show();
        }
        #endregion

        private void ExitSystem_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists("data"))
            {
                Directory.CreateDirectory("data");
            }

            foreach (string item in Directory.GetFiles("data"))
            {
                try
                {
                    File.Delete(item);
                }
                catch { }
            }

            Application.Exit();
        }

        private void Refesh_Click(object sender, EventArgs e)
        {
            LoadGraph("N8", GlobalData.Records[GlobalData.Records.Count - 1].Num);
        }

        #region 其他窗口

        private void 追号计算器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Tool().Show();
        }

        private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("本软件由 周作盛（湖南郴州人）开发，\n\n您可以通过以下联系方式与软件作者取得联系: \n\nQQ:84825263\n\n长沙手机: 18274967383 \n\n郴州手机: 13975739517 \n\n\t\t\t 2013年01月03日", "提示");
        }

        private void 开奖手工登记ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Dj().ShowDialog();
        }

        private void 开奖登记ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UpdateRecord.Enabled = false;
            AddRecord.Enabled = false;

            LoadingManager.Show();

            Thread t = new Thread(LoadRecordsStep1);
            t.IsBackground = true;
            t.Start();
        }
        private int _updateCounts = 0;
        void LoadRecordsStep1()
        {
            _updateCounts = 0;

            _updateCounts = new Lottery.App.BLL.LotteryManager().AutoUpdate();

            Invoke(new LoadDataHandler(LoadRecords));
        }
        void LoadRecords()
        {
            UpdateRecord.Enabled = true;
            AddRecord.Enabled = true;

            LoadingManager.Hide();

            LoadGraph("N8");

            switch (_updateCounts)
            {
                case -1: MessageBox.Show("获取开奖数据失败!请稍候重试!", "提示"); break;
                case 0: MessageBox.Show("当前已经是服务器最新开奖数据!\n\n数据同步比福彩中心发布开奖时间延迟两分钟,请稍等!", "提示"); break;
                default: MessageBox.Show(String.Format("已获取{0}次开奖数据!", _updateCounts), "提示"); break;
            }
        }

        private void 数据维护ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new edit().ShowDialog();
        }

        private void 统计数据维护ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new ResetData().ShowDialog();
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
        }

        #endregion

        #region 走势图处理

        private void GraphN8_Click(object sender, EventArgs e)
        {
            LoadGraph("N8");
        }

        private void GraphA1_Click(object sender, EventArgs e)
        {
            LoadGraph("A1");
        }

        private void GraphA3_Click(object sender, EventArgs e)
        {
            LoadGraph("A3");
        }

        private void LoadGraph(string type)
        {
            this.LoadGraph(type, GlobalData.Records[GlobalData.Records.Count - 1].Num);
        }

        private void LoadGraph(string type, int limit)
        {
            int start = GlobalData.Records.Count - limit;
            int end = GlobalData.Records.Count;

            dgv_List.AutoGenerateColumns = false;
            if (type == "N8")
            {
                List<GraphN8> list = new List<GraphN8>(limit);
                for (int i = start; i < end; i++)
                    list.Add(new GraphN8(GlobalData.Records[i]));
                dgv_List.DataSource = list;
            }
            else if (type == "A1")
            {
                List<GraphA1> list = new List<GraphA1>(limit);
                for (int i = start; i < end; i++)
                    list.Add(new GraphA1(GlobalData.Records[i]));
                dgv_List.DataSource = list;
            }
            else if (type == "A3")
            {
                List<GraphA3> list = new List<GraphA3>(limit);
                for (int i = start; i < end; i++)
                    list.Add(new GraphA3(GlobalData.Records[i]));
                dgv_List.DataSource = list;
            }          

            if (type == "N8")
            {
                dgv_List.Columns[1].Visible = true;
                dgv_List.Columns[2].Visible = true;
                dgv_List.Columns[3].Visible = true;
                dgv_List.Columns[4].Visible = true;
            }
            else
            {
                dgv_List.Columns[1].Visible = false;
                dgv_List.Columns[2].Visible = false;
                dgv_List.Columns[3].Visible = false;
                dgv_List.Columns[4].Visible = false;
            }

            dgv_List.Rows[0].Selected = false;
            //加载完数据后滚动条自动到最下边
            dgv_List.FirstDisplayedScrollingRowIndex = dgv_List.Rows.Count - 1; 
        }
        #endregion

        #region 选择走势图期数

        private void D1Graph_Click(object sender, EventArgs e)
        {
            LoadGraph("N8", GlobalData.Records[GlobalData.Records.Count - 1].Num);
        }

        private void D3Graph_Click(object sender, EventArgs e)
        {
            LoadGraph("N8", 84 * 2 + GlobalData.Records[GlobalData.Records.Count - 1].Num);
        }

        private void D7Graph_Click(object sender, EventArgs e)
        {
            LoadGraph("N8", 84 * 6 + GlobalData.Records[GlobalData.Records.Count - 1].Num);
        }

        private void AllGraph_Click(object sender, EventArgs e)
        {
            LoadGraph("N8", GlobalData.Records.Count);
        }
        #endregion



    }
}
